package com.css.fxfzmh.zcpt.sys.controller;

import com.alibaba.fastjson.JSON;
import com.css.fxfzmh.util.ConnectUtil;
import com.css.fxfzmh.util.PlatformObjectUtils;
import com.css.fxfzmh.web.PlatformSessionContext;
import com.css.fxfzmh.zcpt.sys.entity.SOrg;
import com.css.fxfzmh.zcpt.sys.entity.SUser;
import com.css.fxfzmh.zcpt.sys.entity.UserEntity;
import com.css.fxfzmh.zcpt.sys.entity.common.page.Page;
import com.css.fxfzmh.zcpt.sys.entity.vo.SUserVO;
import com.css.fxfzmh.zcpt.sys.entity.vo.UpdatePasswordVO;
import com.css.fxfzmh.zcpt.sys.service.SUserService;
import com.css.fxfzmh.base.response.RestResponse;
import com.css.fxfzmh.constants.R;
import com.css.fxfzmh.util.CreateGson;
import com.css.fxfzmh.zcpt.sys.service.SysOrgService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fxfz/suser")
@Slf4j
@Validated
public class SUserController {

    @Autowired
    SUserService sUserService;
    @Autowired
    private SysOrgService sysOrgService;


    /**
     * 获取用户信息
     */
    @GetMapping("/getUser")
    public R getUser(@NotBlank(message = "用户id不能为空") @Length(min = 32,message = "用户id不足32位") @RequestParam("userId")  String userId) {
        //String userId = PlatformSessionContext.getUserID();
        String userJson = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        if(PlatformObjectUtils.isEmpty(sUser)){
            sUser = sUserService.getYwUserById(userId);
        }
        return R.ok().put("user",sUser);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserById")
    public R getUserById(@RequestParam("userId") String userId) {
        //String userId = PlatformSessionContext.getUserID();
        String userJson = sUserService.getUserById(userId);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        String org = sysOrgService.getOrgById(sUser.getOrgId());
        SOrg sOrg = CreateGson.createGson().fromJson(org, SOrg.class);
        sUser.setExtend10(sOrg.getName());
        return R.ok().put("user",sUser);
    }


    /**
     * 根据组织机构id获取用户列表（分页）
     */
    @GetMapping("/queryUsersByOrg")
    public RestResponse queryUsersByOrg1(@RequestParam("orgId") String orgId,
                                        @RequestParam("userName") String userName,
                                        @RequestParam("pageSize") Integer pageSize,
                                        @RequestParam("currentPage") Integer currentPage) {
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(orgId)){
            restResponse = RestResponse.fail("缺失组织id!");
            return restResponse;
        }
        try{
            String jsonStr = sUserService.queryUsersByOrg1(orgId,userName,pageSize,currentPage);
            Gson gson = CreateGson.createGson();
            Page page = gson.fromJson(jsonStr, Page.class);
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败!");
        }
        return restResponse;
    }
    /**
     * 根据组织机构id获取用户列表
     */
    @GetMapping("/queryUsersByOrg1")
    public RestResponse queryUsersByOrg(@RequestParam("orgId") String orgId) {
        RestResponse restResponse = null;
        if(org.apache.commons.lang3.StringUtils.isBlank(orgId)){
            restResponse = RestResponse.fail("缺失组织id!");
            return restResponse;
        }
        try{
            String jsonStr = sUserService.queryUsersByOrg(orgId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败!");
        }
        return restResponse;
    }



    /**
     * 修改用户密码
     */
    @PostMapping("/updateUserPassword")
    public R updateUserPassword(@RequestBody UpdatePasswordVO vo)  {
        try{
            String userId = PlatformSessionContext.getUserID();
            // 如果userId为空，则从Session中获取
            if(PlatformObjectUtils.isEmpty(vo.getUserId()) && PlatformObjectUtils.isNotEmpty(userId)){
                vo.setUserId(userId);
            }
            sUserService.updateUserPassword(vo);
        }catch (Exception e){
            return R.error(HttpStatus.SC_UNAUTHORIZED,"修改密码失败");
        }
        return R.ok(HttpStatus.SC_OK,"修改密码成功");
    }

    /**
     * 根据code获取房屋用户信息
     */
    @GetMapping("/getFwUserByCode")
    public RestResponse getFwUserByCode(@RequestParam("userCode") String userCode) {
        RestResponse restResponse = null;


        restResponse = RestResponse.succeed(sUserService.getFwUserByCode(userCode));

        return restResponse;
    }

    /**
     * 根据code获取加固用户信息
     */
    @GetMapping("/getJgUserByCode")
    public RestResponse getJgUserByCode(@RequestParam("userCode") String userCode) {
        RestResponse restResponse = null;


        restResponse = RestResponse.succeed(sUserService.getJgUserByCode(userCode));

        return restResponse;
    }
    /**
     * 获取人口普查的用户信息
     */
    @GetMapping("/getrkUserByCode")
    public RestResponse getRkUserByCode(@RequestParam("userCode") String userCode){
        RestResponse restResponse = null ;
        try {
            restResponse = RestResponse.succeed(sUserService.getRkUserByCode(userCode));
        }catch (Exception e){
            restResponse = RestResponse.fail(PlatformObjectUtils.isNotEmpty(e.getMessage())?e.getMessage():"获取用户失败!");
        }
        return  restResponse ;
    }
    /**
     *  门户支撑平台注册用户
     */
    @PostMapping("/register")
    public RestResponse register(@RequestBody SUserVO suser){
        RestResponse restResponse = null ;
        try {
            restResponse = RestResponse.succeed(JSON.parse(sUserService.addSuser(suser)));

        } catch (Exception e) {
            restResponse = RestResponse.fail(PlatformObjectUtils.isNotEmpty(e.getMessage())?e.getMessage():"注册失败!");
        }

        return restResponse;
    }
}
